# coding: utf-8
from PySide6.QtWidgets import QWidget, QVBoxLayout
from qfluentwidgets import GroupHeaderCardWidget, PushButton, ComboBox, PrimaryPushButton, FluentIcon as FIF, Pivot, \
    LineEdit

from common import SingleIcon
from components import VerticalScrollArea, SlidingStackedWidget, FileGroupCard, ListGroupCard


class BasicSettingWidget(VerticalScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.headerCard = GroupHeaderCardWidget(self.tr('基本设置'), self)
        self.entranceBtn = PushButton(self.tr('选择'), self)
        self.entranceBtn = self.headerCard.addGroup(SingleIcon.Python, self.tr('入口脚本'),
                                                    self.tr('设置程序的入口脚本'), self.entranceBtn)
        self.iconBtn = PushButton(self.tr('选择'), self)
        self.iconBtn = self.headerCard.addGroup(SingleIcon.Logo, self.tr('图标'),
                                                self.tr('设置程序的图标'), self.iconBtn)
        self.fileTypeComboBox = ComboBox(self)
        self.fileTypeCard = self.headerCard.addGroup(SingleIcon.Packing, self.tr('打包方式'),
                                                     self.tr('设置程序的打包方式'), self.fileTypeComboBox)
        self.terminalComboBox = ComboBox(self)
        self.terminalCard = self.headerCard.addGroup(SingleIcon.Terminal, self.tr('运行终端'),
                                                     self.tr('启动软件时使用的终端'), self.terminalComboBox)
        self.nameLineEdit = LineEdit(self)
        self.nameCard = self.headerCard.addGroup(FIF.CHAT, self.tr('软件名称'),
                                                 self.tr('设置软件的名称'), self.nameLineEdit, 1)
        self.versionLineEdit = LineEdit(self)
        self.versionCard = self.headerCard.addGroup(FIF.PENCIL_INK, self.tr('版本号'),
                                                    self.tr('设置软件的版本号'), self.versionLineEdit, 1)
        self.startBtn = PrimaryPushButton(FIF.PLAY_SOLID, self.tr('编译'), self)
        self.startCard = self.headerCard.addGroup(SingleIcon.Rocket, self.tr('编译'),
                                                  self.tr('点击编译按钮开始编译👉'), self.startBtn)
        self.__initWidgets()

    def __initWidgets(self):
        self.setObjectName('BasicSettingWidget')

        self.fileTypeComboBox.addItem(self.tr('单文件'), userData='onefile')
        self.fileTypeComboBox.addItem(self.tr('文件夹'), userData='onedir')

        self.terminalComboBox.addItem(self.tr('始终显示终端'), userData=True)
        self.terminalComboBox.addItem(self.tr('不显示终端'), userData=False)

        self.nameLineEdit.setPlaceholderText(self.tr('软件名称'))
        self.versionLineEdit.setPlaceholderText(self.tr('版本号'))
        self.nameLineEdit.setClearButtonEnabled(True)
        self.versionLineEdit.setClearButtonEnabled(True)

        self.setLayoutContentsMargins(0, 0, 0, 0)
        self.addWidget(self.headerCard)

    def getCommandLineArgs(self):
        pass


class AdditionalFileWidget(VerticalScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.fileGroupCard = FileGroupCard(self)

        self.setObjectName("AdditionalFileWidget")
        self.setLayoutContentsMargins(0, 0, 0, 0)
        self.addWidget(self.fileGroupCard, 1)


class HideableWidget(VerticalScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.listWidget = ListGroupCard(self)

        self.setObjectName('HideableWidget')
        self.setLayoutContentsMargins(0, 0, 0, 0)
        self.addWidget(self.listWidget, 1)


class SettingInterface(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.mapping = {}
        self.verticalLayout = QVBoxLayout(self)
        self.pivot = Pivot(self)
        self.stackedWidget = SlidingStackedWidget(self)
        self.basicSettingWidget = BasicSettingWidget(self)
        self.additionalFileWidget = AdditionalFileWidget(self)
        self.hideableWidget = HideableWidget(self)

        self.__initWidgets()

    def __initWidgets(self):
        self.setObjectName('SettingInterface')

        self.verticalLayout.setContentsMargins(0, 0, 10, 10)
        self.verticalLayout.addWidget(self.pivot)
        self.verticalLayout.addWidget(self.stackedWidget, 1)

        self.pivot.currentItemChanged.connect(lambda key: self.stackedWidget.setCurrentWidget(self.mapping[key]))
        self.addItem(self.tr('基本设置'), self.basicSettingWidget)
        self.addItem(self.tr('附加文件'), self.additionalFileWidget)
        self.addItem(self.tr('隐式导入'), self.hideableWidget)
        self.pivot.setCurrentItem(self.basicSettingWidget.objectName())

    def addItem(self, text: str, widget: QWidget):
        routeKey = widget.objectName()
        if not routeKey:
            raise ValueError('widget must have objectName')
        self.pivot.addItem(routeKey, text)
        self.mapping[routeKey] = widget
        self.stackedWidget.addWidget(widget)
